<template>
  <div>
    <el-form :model="formData" label-width="90px" :rules="FormRules" ref="addForm">
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="formData.companyName" placeholder="请输入公司名称"></el-input>
      </el-form-item>
      <el-form-item label="公司描述" prop="description">
        <el-input
          v-model="formData.description"
          placeholder="请输入公司描述"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input type="number" v-model="formData.sort" placeholder="请输入公司排序的数字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handSave()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addCompany, editCompany } from "../../../api/baseSetting";
export default {
  data() {
    var checkSort = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入排序"));
      }
      if (!Number.isInteger(value * 1)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };
    return {
      formData: {
        companyId: "",
        companyName: "",
        description: "",
        sort: ""
      },
      FormRules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" }
        ],
        description: [
          { required: true, message: "请输入公司描述", trigger: "blur" },
          { max: 50, message: "长度小于50 个字符", trigger: "blur" }
        ],
        sort: [{ validator: checkSort, trigger: "blur" }]
      }
    };
  },
  props: {
    sendFormData: {
      num: {
        type: Object
      }
    }
  },
  methods: {
    //点击保存
    handSave() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          if (this.formData.companyId) {
            //编辑
            editCompany(this.formData).then(res => {
              this.$message({
                message: "成功",
                type: "success"
              });
              this.$store.commit("setCompanyList", []);
              this.$emit("hideTotas");
            });
          } else {
            //新增
            addCompany(this.formData).then(res => {
              this.$message({
                message: "成功",
                type: "success"
              });
              this.$store.commit("setCompanyList", []);
              this.$emit("hideTotas");
            });
          }
        }
      });
    }
  },
  mounted() {
    console.log(this.sendFormData);
    this.formData = {
      companyId: this.sendFormData.companyId,
      companyCode: this.sendFormData.companyCode,
      companyName: this.sendFormData.companyName,
      description: this.sendFormData.description,
      sort: this.sendFormData.sort
    };
  }
};
</script>

<style lang="less" scoped>
</style>
